package com.vodone.v1pin.core.service
{
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.http.mxml.HTTPService;
	
	public class HttpJsonDelegate
	{
		public function HttpJsonDelegate( service:HTTPService , filter:IHttpParamsFilter = null , responder:IResponder = null )
		{
			this.service = service;
			this.filter = filter;
			this.responder = responder;			
		}
		
		public var service:HTTPService;
		/**
		 * 统一处理http出口 
		 */
		public var filter:IHttpParamsFilter; 
		/**
		 * 统一处理http入口
		 */		
		public var responder:IResponder;
		
		/**
		 * 
		 * @param params 请求参数
		 * @param resultHandler 成功结果处理函数，签名为resultHandler（ response:Object , ... ); 第一个参数为解析后的返回值对象，后面的参数可以根据情况扩展，如传入上下文等。
		 * @param faultHandler 错误结果处理函数
		 * @param handlerArgs 自定义扩展的参数
		 * 
		 */		
		public function send( params:Object , resultHandler:Function , faultHandler:Function = null, handlerArgs:Array = null ):void
		{
			params.rtp = String(Math.random() * 65535);
			params =  filter ? filter.filterParams( params ) : params;
			var token:AsyncToken = service.send( params );
			if( responder )
			{
				token.addResponder( responder );
			}
			token.addResponder( new JsonResponder( resultHandler , faultHandler , handlerArgs  ) );
		}
	}
}